package org.mashart.pink.m.gui;

import com.sun.lwuit.*;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.plaf.Style;
import com.sun.lwuit.plaf.UIManager;

/**
 * Ditial Meter, displays the string value of a variable
 */
public class DigitalMeter extends Component{
    /**
     * Title of the variable
     */
    private String title;
    /**
     * String value of the variable
     */
    private String text;
    /**
     * Font of the title
     */
    private Font titleFont;
    /**
     * Font of the text value
     */
    private Font textFont;
    /**
     * Color setting
     */
    static final private int titleColor = 0xffffff;
    static final private int textColor = 0x00ff00;
    static final private int bgColor = 0x000000;

    /**
     * Constructor.
     * @param tt title
     */
    DigitalMeter ( String tt ){
        super();
        // initialize the title
        title = tt;
        // not focusable
        setFocusable(false);

        // create font for title and text
        titleFont = Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL);
        textFont = Font.createSystemFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_LARGE);
    }

    /**
     * Get the title.
     * @return title
     */
    public String getTitle() {
        return title;
    }

    /**
     * Set the title.
     * The Digital Meter will be repainted.
     * @param tt title
     */
    public void setTitle ( String tt ) {
        title = tt;
        repaint();
    }

    /**
     * Get the text value.
     * @return the text value
     */
    public String getText() {
        return text;
    }

    /**
     * Set the text value.
     * The Digital Meter will be repainted.
     * @param tx the text value
     */
    public void setText ( String tx ) {
        text = tx;
        repaint();
    }

    /**
     * Paint the digital meter on screen.
     * @param g the component graphics
     */
    public void paint ( Graphics g ) {
        // get the style
        UIManager.getInstance().getLookAndFeel().setFG(g, this);
        Style style = getStyle();

        // paint the backgound
        g.setColor(bgColor);
        g.fillRoundRect(getX(), getY(), getWidth(), getHeight(), 12, 12);

        // draw the title
        if ( title != null ) {
            g.setFont(titleFont);
            g.setColor(titleColor);
            g.drawString( title, getX() + style.getPadding(LEFT), getY() + style.getPadding(TOP) );
        }
        
        // draw the text
        if ( text != null ) {
            g.setFont(textFont);
            g.setColor(textColor);
            g.drawString( text, getX() + style.getPadding(LEFT), getY()  + style.getPadding(TOP) + g.getFont().getHeight() );
        }
    }

    /**
     * Calculates the preferred size based on component content.
     * @return the calculated preferred size based on component content
     */
    protected Dimension calcPreferredSize() {
        // get the font
        Style style = getStyle();
        Font fnt = style.getFont();

        // calculate the width of title and text
        int titleWidth = (title == null) ? 0 : fnt.stringWidth(title);
        int textWidth = (text == null) ? 0 : fnt.stringWidth(text);
        // calculate the width of this component
        int width = Math.max( titleWidth, textWidth );
        width += style.getPadding(Component.LEFT) + style.getPadding(Component.RIGHT);
        // calculate the height of this component
        int height = fnt.getHeight() + fnt.getHeight();
        height += style.getPadding(Component.TOP) + style.getPadding(Component.BOTTOM);
        return new Dimension(width, height);
    }
}
